package com.etonedu.meals.http.xutils;

import com.blankj.utilcode.util.LogUtils;
import com.etonedu.meals.utils.Utils;

import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class NewXUtilsRequest {
    private NewXUtilsResponse newXUtilsResponse;

    private String url;
    private String type;
    private String header;
    private String bodyContent;

    public NewXUtilsRequest(String header, String bodyContent) {
        this.header = header;
        this.bodyContent = bodyContent;
    }
    public void requet(final String url, final String type, HttpMethod method, final NewXUtilsResponse newXUtilsResponse) {
        RequestParams requestParams = new RequestParams(url);
        requestParams.setConnectTimeout(60*1000);
        requestParams.setReadTimeout(60*1000);
        if (!Utils.isEmpty(header)) {
            requestParams.setHeader("authStr",header);
        }
        requestParams.setBodyContent(bodyContent);

        x.http().request(method, requestParams, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                LogUtils.e("url = "+url+",result = "+result);
                if (newXUtilsResponse != null) {
                    newXUtilsResponse.response(type,result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
                LogUtils.e("url = "+url);
                LogUtils.e(ex);
                if (newXUtilsResponse != null) {
                    newXUtilsResponse.error(type,ex.getMessage());
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {
                cex.printStackTrace();
                LogUtils.e(cex);
                if (newXUtilsResponse != null) {
                    newXUtilsResponse.error(type,cex.getMessage());
                }
            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void requet8(final String url, final String type, HttpMethod method, final NewXUtilsResponse newXUtilsResponse) {
        RequestParams requestParams = new RequestParams(url);
        requestParams.setConnectTimeout(8*1000);
        requestParams.setReadTimeout(8*1000);
        if (!Utils.isEmpty(header)) {
            requestParams.setHeader("authStr",header);
        }
        requestParams.setBodyContent(bodyContent);

        x.http().request(method, requestParams, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                LogUtils.e("url = "+url+",result = "+result);
                if (newXUtilsResponse != null) {
                    newXUtilsResponse.response(type,result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
                LogUtils.e(ex);
                if (newXUtilsResponse != null) {
                    newXUtilsResponse.error(type,ex.getMessage());
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {
                cex.printStackTrace();
                LogUtils.e(cex);
                if (newXUtilsResponse != null) {
                    newXUtilsResponse.error(type,cex.getMessage());
                }
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
